//
//  LoginDetailViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/20.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "LoginDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "CFMacro.h"

@interface LoginDetailViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UILabel *nickName;
// 接口需要用到的参数
@property (weak, nonatomic) IBOutlet UILabel *session_id;
@property (weak, nonatomic) IBOutlet UIButton *myDjRoom;

@property (strong, nonatomic) UIActionSheet *actionSheet;

@end

@implementation LoginDetailViewController

#warning LoginDetailViewController send noti
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"login"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hidePlayerView"
                                                        object:@"playerView"
                                                      userInfo:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showPlayerView"
                                                        object:@"playerView"
                                                      userInfo:nil];
}
#pragma mark - 进入我的主播空间
- (IBAction)goToMyDJRoom:(id)sender {
    NSLog(@"进入我的主播空间, 待实现");
}
#pragma mark - 退出登录
- (IBAction)signOff:(id)sender {
    NSLog(@"实现退出登录操作");
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"user"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"signOff"
                                                        object:@"signOff"];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setUserDic:(NSDictionary *)userDic {
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *userDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
    /**
     * 添加手势选取系统相册照片
     */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callActionSheetFunc)];
    [_avatarView addGestureRecognizer:tap];
    _avatarView.userInteractionEnabled = YES;
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:userDic[@"logo_url"]]
                   placeholderImage:[UIImage imageNamed:kPlacoHolderImage]];
    _avatarView.layer.cornerRadius = 45;
    _avatarView.layer.masksToBounds = YES;
    _nickName.text = userDic[@"nickname"];
    _session_id.text = [NSString stringWithFormat:@"用户ID:%@", userDic[@"kugou_id"]];
}
/**
 * 调用ActionSheet
 */
- (void)callActionSheetFunc{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil, nil];
    }else{
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil, nil];
    }
    self.actionSheet.tag = 1000;
    [self.actionSheet showInView:self.view];
}
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 1000) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //来源:相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    //来源:相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
            }
        } else {
            if (buttonIndex == 2) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.navigationBar.barTintColor = [UIColor blackColor];
        imagePickerController.sourceType = sourceType;
        [self presentViewController:imagePickerController animated:YES completion:^{
#warning LoginDetailViewController send noti
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hidePlayerView"
                                                                object:@"playerView"
                                                              userInfo:nil];
        }];
    }
}
//选择照片完成之后的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //info是所选择照片的信息
    NSLog(@"%@",info);
    //刚才已经看了info中的键值对，可以从info中取出一个UIImage对象，将取出的对象赋给按钮的image
    UIImage *resultImage = [info objectForKey:UIImagePickerControllerEditedImage];
    _avatarView.image = resultImage;
    
    //使用模态返回到软件界面
    [self.navigationController dismissViewControllerAnimated:YES completion:^{}];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
